package com.xiaoxing.zhinenghuacangku_liaocheng.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.zhinenghuacangku_liaocheng.R;
import com.xiaoxing.zhinenghuacangku_liaocheng.R2;
import com.xiaoxing.zhinenghuacangku_liaocheng.di.component.DaggerLiaoCheng_YiKuQingZhangComponent;
import com.xiaoxing.zhinenghuacangku_liaocheng.di.module.LiaoCheng_YiKuQingZhangModule;
import com.xiaoxing.zhinenghuacangku_liaocheng.mvp.contract.LiaoCheng_YiKuQingZhangContract;
import com.xiaoxing.zhinenghuacangku_liaocheng.mvp.presenter.LiaoCheng_YiKuQingZhangPresenter;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import butterknife.BindView;
import butterknife.OnClick;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.base.BaseConstants.SCAN_REQUEST_CODE;
import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.xiaoxing.zhinenghuacangku_liaocheng.mvp.ui.activity.LiaoCheng_ReturnLibraryActivity.REQUEST_WU_LIAO;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_LIAO_CHENG_ACTIVITY_YI_KU_QING_ZHANG)
public class LiaoCheng_YiKuQingZhangActivity extends BaseActivity<LiaoCheng_YiKuQingZhangPresenter> implements LiaoCheng_YiKuQingZhangContract.View {


    @BindView(R2.id.btn_scan_wu_liao)
    Button btnScanWuLiao;
    @BindView(R2.id.btn_chong_xin_yi_ku)
    Button btnChongXinYiKu;
    private String mid = "";

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLiaoCheng_YiKuQingZhangComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .liaoCheng_YiKuQingZhangModule(new LiaoCheng_YiKuQingZhangModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.zhi_neng_cang_ku_liao_cheng_activity_yi_ku_qing_zhang; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.zhi_neng_cang_ku_lin_yi_activity_yi_ku_qing_zhang));
    }

    @OnClick({R2.id.btn_scan_wu_liao, R2.id.btn_chong_xin_yi_ku})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_scan_wu_liao) {

            Intent newIntent = new Intent(this, LiaoCheng_ScanQRCodeActivity.class);
            mSharedPreferencesHelper.putInt(SCAN_REQUEST_CODE, REQUEST_WU_LIAO);
            startActivityForResult(newIntent, REQUEST_WU_LIAO);
        } else if (i == R.id.btn_chong_xin_yi_ku) {


            if (TextUtils.isEmpty(mid)) {
                ArmsUtils.makeText(getContext(), "请先扫描物料二维码");
                return;
            }
//            HashMap<String, String> map = new HashMap<>();
//            map.put("mid", mid);
//            mPresenter.sendMaterialMoveOrderAgain(map);
            sendMaterialMoveOrderAgain(mid);
        }
    }

    private void sendMaterialMoveOrderAgain(String mid) {

        showLoading();
        String methodName = "SendMaterialMoveOrderAgain";
        SoapParams params = new SoapParams();
        params.put("mid", mid);
        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (result != null) {

                        if (result.equals("1")) {
                            ArmsUtils.makeText(getContext(), "成功");
                        } else if (result.equals("0")) {
                            ArmsUtils.makeText(getContext(), "失败");
                        }
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog();

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == REQUEST_WU_LIAO) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                mid = extras.getString(BaseConstants.SCAN_RESULT);
            }

        }

    }

    @Override
    public void sendMaterialMoveOrderAgainSuccess(String entityData) {

        if (entityData != null) {

            if (entityData.equals("1")) {
                ArmsUtils.makeText(getContext(), "成功");
            } else if (entityData.equals("0")) {
                ArmsUtils.makeText(getContext(), "失败");

            }
        }
    }
}
